<template>
  <div class="PositionRecReason">
    <!-- <h4>你是从哪个渠道认识候选人的：</h4>
    <a-select style="width: 100%" v-model="channel">
      <a-select-option value="1" >1</a-select-option>
      <a-select-option value="1" >2</a-select-option>
    </a-select>
    <h4 class="mt">推荐理由：</h4>
    <a-textarea style="height: 150px" v-model="remark"></a-textarea> -->
    <a-form :form="form" class="ResumeUpdate">
      <a-form-item v-for="record in templates.fields" :key="record.fullPath">
        <template slot="label" class="disposeLabel">
          <a-tooltip>
            <template slot='title'>
              {{ record.title }}
            </template>
            {{ record.title }}
          </a-tooltip>
        </template>
        <component
          v-decorator="[record.fullPath, {rules: [{ required: record.required, message: $t('common.pleaseEnter') + record.title }]}]"
          :is="record.type"
          :keys="record.key"
          :placeholders="record.displayName"
          :url='record.url'
          :values='record.enums'
          :unlimitedNumberValue="false"
          :isDisabled="record.disabled" />
    </a-form-item>
  </a-form>
  </div>
</template>
<script>
import loGet from 'lodash.get'
import loSet from 'lodash.set'
import Editor from '@/components/Editor/Editor'
import TEXT_FIELD from '@/components/formComponents/TextField/TextField'
import MOBILE from '@/components/formComponents/Phone/Phone'
import EMAIL from '@/components/formComponents/Email/Email'
import RADIO_GROUP from '@/components/formComponents/RadioGroup/RadioGroup'
import CHECKBOX_GROUP from '@/components/formComponents/Checkbox/Checkbox'
import DATE from '@/components/formComponents/FormattingDate/FormattingDate'
import INT from '@/components/formComponents/Int/Int'
import TEXT_AREA from '@/components/formComponents/TEXT_AREA/TEXT_AREA'
import SELECT from '@/components/formComponents/Select/Select'
import DEPARTMENT from '@/components/formComponents/Department/Department'
import SELECT_URL from '@/components/formComponents/SelectUrl/SelectUrl'
import RICH_TEXTAREA from '@/components/formComponents/RichTextarea/RichTextarea'
import JOB_TYPE from '@/components/formComponents/JobType/JobType'
import CHECKBOX from '@/components/formComponents/jobTypeCheckbox/jobTypeCheckbox'
import ResumeUpdateDiv from '@/components/resume/ResumeUpdateDiv'
import SCHOOLSELECTOR from '@/components/formComponents/schoolSelector/schoolSelector'
import MobileInternational from '@/components/formComponents/MobileInternational/MobileInternational'
import { recommendReasonTemplate } from '@/api/resume'
export default {
  name: 'PositionRecReason',
  components: { Editor, TEXT_FIELD, MOBILE, EMAIL, RADIO_GROUP, CHECKBOX_GROUP, DATE, INT, TEXT_AREA, SELECT, DEPARTMENT, SELECT_URL, RICH_TEXTAREA, JOB_TYPE, CHECKBOX, ResumeUpdateDiv, SCHOOLSELECTOR, MobileInternational },
  data () {
    return {
      loGet,
      loSet,
      form: this.$form.createForm(this),
      templates: {}
    }
  },
  // props: [ 'enterpriseId', 'positionId', 'recommendInfo', 'positionInfo', 'resumeData', 'searchBtn' ],
  props: {
    enterpriseId: { // 企业id
      type: String,
      required: true
    },
    positionId: { // 职位id
      type: String,
      required: true
    },
    recommendInfo: { // 推荐理由数据
      type: Object,
      required: false,
      default: function () {
        return {}
      }
    },
    positionInfo: { // 职位详细信息 用于回显模板数据
      type: Object,
      required: false,
      default: function () {
        return {}
      }
    },
    resumeData: { // 简历详细信息 用于回显模板数据
      type: Object,
      required: false,
      default: function () {
        return {}
      }
    },
    searchBtn: { // 简历详细信息 用于回显模板数据
      type: Number,
      required: false
    }
  },
  watch: {
    searchBtn: {
      handler (newValue, oldValue) {
        if (newValue === 3) {
          this.setFileds()
        }
      }
    }
  },
  created () {
    this.getTemplates()
  },
  mounted () {
  },
  methods: {
    filterTmp (templates) { // 设置职位字段、简历字段 disabled
      if (!templates.fields) return templates
      templates.fields.forEach(item => {
        let path = item.fullPath.substring(0, item.fullPath.indexOf('.'))
        if (path === 'position' || path === 'application') {
          item.required = false
          item.disabled = true
          item.fullPath = item.fullPath.substring(item.fullPath.indexOf('.') + 1)
        }
      })
      return templates
    },
    setFileds () {
      this.$nextTick(() => {
        this.arrangeResume(this.resumeData)
        if (this.recommendInfo) {
          this.arrangeReason(this.recommendInfo)
          this.form.setFieldsValue({ ...this.recommendInfo, ...this.resumeData, ...this.positionInfo })
        } else {
          this.form.setFieldsValue({ ...this.resumeData, ...this.positionInfo })
        }
      })
    },
    async getTemplates () {
      let params = {
        enterpriseId: this.enterpriseId,
        positionId: this.positionId,
        isRPO: this.positionInfo.isRPO || 'N'
      }
      let res = await recommendReasonTemplate(params)
      if (res.code === 200) {
        this.templates = this.filterTmp(res.data)
        this.setFileds()
      } else {
        this.$message.error(res.message)
      }
    },
    arrangeReason (data) { // 处理理由字段回显
      this.templates.fields.forEach((item) => {
        // template.fields.forEach(item => {
        if (item.enums) {
          item.enums.some(v => {
            if (this.loGet(data, item.fullPath) && this.loGet(data, item.fullPath).id === v.id) {
              this.loSet(data, item.fullPath, v)
              return true
            }
          })
        }
        // })
      })
    },
    arrangeResume (data) { // 处理简历字段数据
      this.templates.fields.forEach((item) => {
        // template.fields.forEach(item => {
        if (item.enums) {
          item.enums.some(v => {
            if (item.type === 'LIST') {
              data[item.path] && data[item.path].forEach(val => {
                // 设置list数据
                if (this.loGet(val, item.path) && this.loGet(val, item.path).id === v.id) {
                  this.loSet(val, item.path, v)
                  return true
                }
              })
            } else {
              // 设置数据数据，将v.id取出来
              if (this.loGet(data, item.fullPath) && this.loGet(data, item.fullPath).id === v.id) {
                this.loSet(data, item.fullPath, v)
                return true
              }
            }
          })
        }
        // })
      })
    },
    arrangeData (values) {
      this.templates.fields.forEach(item => {
        if (item.enums) {
          item.enums.some((v, index) => {
            // if (this.loGet(values, item.fullPath) && this.loGet(values, item.fullPath) instanceof Array && this.loGet(values, item.fullPath).some(item => item.id === v.id)) {
            //   tmp.push(v.id)
            // } else
            if (this.loGet(values, item.fullPath) && this.loGet(values, item.fullPath).id === v.id) {
              this.loSet(values, item.fullPath, v.id)
              return true
            }
          })
        }
      })
      return values
    },
    sendData () { // 上送数据传递到父组件
      return new Promise((resolve, reject) => {
        this.form.validateFields((error, values) => {
          if (!error) {
            // 处理数据
            resolve(this.arrangeData({ recommendTemplateInfo: values.recommendTemplateInfo }))
          } else {
            reject(error)
          }
        })
      }).catch((e) => {
        console.log(e)
      })
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '../../styles/var.styl'
.PositionRecReason
  h4
    font-weight bold
    font-size 16px
    margin-bottom 6px
    &.mt
      margin-top 12px
</style>
